SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SaveQuestionnaireItem]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SaveQuestionnaireItem]
GO


/***************************************************************************
* Name:			SaveQuestionnaireItem
* Purpose:		Insert/Update a question
*
* PARAMETERS
* Name			Description					
* -------------		-----------------------------------------------------------
* @id			unique id (0 for new records to be inserted)
* @versionID		id of the questionnaire version to which the record is to be mapped
* @itemName		name of the question (unique)
* @itemText		question text
* @itemType		type of the question (must be one of: Label, TextBox, DropDownList, 
			RadioButtonList, CheckBoxList, MultiInput, DropDownMultiSelect)
* @itemOder		sequence number for the question (while inserting a new record, 
			itemorder is calculated based on the existing records)
* @hasSeparator		flag that indicates whether or not a horizontal line is displayed after the question
* @indent		indicates whther or not the question should be indented
* @parentName		name of the parent question, if any
* @parentValue		answer for the parent question, if any
* @required		indicates whether or not the question is required
* @validationMessage	text to be displayed to the user upon failure of Questionnaire Validation process
* @optionsRefDomain	RefDomain for options

***************************************************************************/
CREATE PROCEDURE [dbo].[SaveQuestionnaireItem] (

@id INT,
@versionID INT,
@itemName varchar(30),
@itemText varchar(4000),
@itemType varchar(30),
@itemOrder  INT,
@hasSeparator bit,
@indent bit,
@parentName varchar(30),
@parentValue varchar (30),
@required bit,
@validationMessage varchar(250),
@optionsRefDomain varchar(50) = null

)

AS

BEGIN

	SET NOCOUNT ON

		-- if it's zero then insert
		IF @id = 0

		BEGIN


			INSERT INTO QuestionnaireItem ( 
							 VersionID,
							 ItemName, 
						         ItemText, 
                                                         ItemType, 
                                                         HasSeparator, 
							 Required, 
							 ValidationMessage,  
							 Indent, 
							 ItemOrder,
							 ParentName,
							 ParentValue,
							 OptionsRefDomain)
					VALUES          (  
							@versionID,
							@itemName,
							@itemText , 
							@itemType , 
							@hasSeparator, 
							@required, 
							@validationMessage, 
							@indent, 
							@itemOrder,
							@parentName,
							@parentValue,
							@optionsRefDomain)
			IF @@ERROR <> 0
			BEGIN
				RAISERROR('Unable to Add Questionnaire Item', 16, 1)
				RETURN
			END
			
			SELECT IDENT_CURRENT('QuestionnaireItem')
			RETURN

		END

		ELSE
		BEGIN

			DECLARE @oldItemName VARCHAR(30)
			SELECT @oldItemName = ItemName FROM QuestionnaireItem WHERE ID=@id

			UPDATE QuestionnaireItem 
			SET
			VersionID =  @versionID,
			ItemName =  @itemName,
                        ItemText   =  @itemText , 
			ItemType  =  @itemType , 
			HasSeparator = @hasSeparator, 
			Required = @required, 
			ValidationMessage = @validationMessage,
			Indent = @indent, 
			ItemOrder     = @itemOrder,
			ParentName = @parentName,
			ParentValue = @parentValue,
			OptionsRefDomain = @optionsRefDomain
			WHERE ID = @id


			Update QuestionnaireItem 
			SET 
				ParentName = @itemName
			WHERE 
				VersionID =  @versionID
				AND ParentName = @oldItemName
			
			IF @@ERROR <> 0
			BEGIN
				RAISERROR('Unable to Update Questionnaire Item', 16, 1)
				RETURN
			END

			SELECT @id	
			RETURN
		END

		                                                                                           
END
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

